﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class LoanAnalizComments : System.Web.UI.UserControl
{
    string appPath = string.Empty;
 
    int parameterX = 0;
    public int ParameterX
    {
        set { parameterX = value; }
    }

    int parameterY = 0;
    public int ParameterY
    {
        set { parameterY = value; }
    }
    
    int mLoanID = 0;
    public int LoanID
    {        
        get { return ViewState[this.ClientID + "LoanID"] != null ? (int)ViewState[this.ClientID + "LoanID"] : 0; }
        set
        {
            ViewState[this.ClientID + "LoanID"] = value; 
            commentTextBox.Text = string.Empty;
            bodyMessageDiv.InnerHtml = string.Empty;
            FIllAllComments();
        }
    }

    public void FIllAllComments()
    {
        int loanID = this.LoanID;

        if (loanID != 0)
        {
            DataTable messages = Utils.ModuleCredits().GetGetLoanAnalizComments(loanID);
            bodyMessageDiv.InnerHtml = GetHTMLTextWithMessage(messages);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bodyMessageDiv.Style.Add("overflow-y", "scroll");
            bodyMessageDiv.Style.Add("padding", "5px");
        }
    }   

    protected void SaveMessageButton_Click(object sender, EventArgs e)
    {
        string comment = commentTextBox.Text.Trim();

        if (this.LoanID != 0 && !comment.Equals(string.Empty) && Utils.ModuleCredits().AddAnalizComment(this.LoanID, comment, Utils.UserObject().UserID))
        {
            FIllAllComments();
            commentTextBox.Text = string.Empty;
        }
        else
        {
            Utils.InfoText(this.Page, "Atentie! Eroare in sistem!", "Datele nu au fost salvate. " + Utils.ModuleCredits().LastError);
        }
    }

    protected string GetHTMLTextWithMessage(DataTable sourceMessagesTable)
    {
        string result = string.Empty;

        if (sourceMessagesTable != null && sourceMessagesTable.Rows.Count > 0)
        {
            for (int i = 0; i < sourceMessagesTable.Rows.Count; i++)
            {
                string userAnddateModification = sourceMessagesTable.Rows[i]["UserDate"].ToString();
                string Comment = sourceMessagesTable.Rows[i]["Comment"].ToString();

                result += "<div style=\"text-align:left; padding-left:20px;\">" + Comment + "</div>";
                result += "<div style=\"text-align:right; width:100%; float:left;\">" + userAnddateModification + "</div>";
                result += "<hr>";      
                result += "<div style=\"clear:both;\"></div>";
            }
        }

        return result;
    }
}
